package com.firebird.app.kotlin.stock.ui.allocation_manage

import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import butterknife.BindView
import butterknife.OnClick
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.AllocationManageBean
import com.firebird.app.kotlin.stock.data.AllocationManageBean.RespDataBean.DataBean
import com.firebird.app.kotlin.stock.myview.state_layout.OtherView
import com.google.gson.Gson
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.ui.allocation_manage.contract.AllocationManageContract
import com.firebird.app.kotlin.stock.ui.allocation_manage.presenter.AllocationManagePresenter
import com.firebird.app.kotlin.stock.ui.detailsallocatin.DetailsAllocationActivity
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.SelectorUtils
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil
import java.util.*


class AllocationManageActivity : BaseMvpActivity<AllocationManagePresenter?>(), AllocationManageContract.View {
    @BindView(R.id.back)
    var back: LinearLayout? = null
    @BindView(R.id.tv_title)
    var tvTitle: TextView? = null
    @BindView(R.id.tv_state)
    var tvState: TextView? = null
    @BindView(R.id.tv_all)
    var tvAll: TextView? = null
    @BindView(R.id.recycler)
    var recycler: RecyclerView? = null
    @BindView(R.id.otherview)
    var otherview: OtherView? = null
    @BindView(R.id.refresh)
    var refresh: PullToRefreshLayout? = null
    private val list: MutableList<DataBean> = ArrayList()
    private val stateList: MutableList<String> = ArrayList()
    private val typeList: MutableList<String> = ArrayList()
    private var adp: Adp? = null
    private var page = 0
    private var dialog: Dialog? = null
    private var strState = ""
    private var strType = ""
    private val bundle = Bundle()
    private var strJson: String? = null
    override val layoutId: Int
        get() = R.layout.activity_allocation_manage

    override fun initView() {
        tvTitle!!.text = "配资管理"
        otherview!!.setHolder(mHolder)
        mPresenter = AllocationManagePresenter()
        mPresenter.attachView(this)
        refresh!!.setRefreshListener(object : BaseRefreshListener {
            override fun refresh() {
                page = 0
                mPresenter.getDataFresh(strState, strType, page.toString() + "", Config.PAGESIZE)
            }

            override fun loadMore() {
                page++
                mPresenter.getDataLoadMore(strState, strType, page.toString() + "", Config.PAGESIZE)
            }
        })
    }

    override fun onStart() {
        super.onStart()
        page = 0
        mPresenter.getData(strState, strType, page.toString() + "", Config.PAGESIZE)
    }

    override fun showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this)
    }

    override fun hideLoading() {
        DialogUtils.closeDialog(dialog)
    }

    override fun onError(throwable: Throwable?) {
        DialogUtils.closeDialog(dialog)
        refresh!!.finishRefresh()
        refresh!!.finishLoadMore()
    }

    fun onSuccess(bean: AllocationManageBean) {
        initData(bean)
    }

    fun onSuccessFresh(bean: AllocationManageBean) {
        refresh!!.finishRefresh()
        initData(bean)
    }

    fun onSuccessLoadMore(bean: AllocationManageBean) {
        refresh!!.finishLoadMore()
        if (bean.respCode == Config.SUCCESS) {
            list.addAll(bean.respData!!.data!!)
            //延迟更新数据，避免卡顿
            Handler().postDelayed({ adp.notifyItemRangeChanged(0, bean.respData!!.data!!.size) }, 500)
            if (bean.respData!!.data!!.size == 0) {
                ToastUtil.showToast(this, "没有更多数据")
            }
        } else if (bean.respCode == Config.TOKENOUT) {
            ShowDialogUtils.restLoginDialog(this)
        } else {
            ToastUtil.showToast(this, bean.respDesc)
        }
    }

    @OnClick(R.id.back, R.id.tv_state, R.id.tv_all)
    fun onViewClicked(view: View) {
        when (view.id) {
            R.id.back -> finish()
            R.id.tv_state -> chooseState()
            R.id.tv_all -> chooseType()
        }
    }

    private fun chooseState() {
        stateList.clear()
        stateList.add("操盘中")
        stateList.add("申请结束")
        stateList.add("终止")
        val pvNoLinkOptions: OptionsPickerView<*> = OptionsPickerBuilder(this, OnOptionsSelectListener { options1, options2, options3, v ->
            tvState!!.text = stateList[options1]
            strState = (options1 + 1).toString() + ""
            page = 0
            mPresenter.getData(strState, strType, page.toString() + "", Config.PAGESIZE)
        }).setCancelColor(Color.parseColor("#999999"))
                .setSubmitColor(Color.parseColor("#ff3025"))
                .setTitleBgColor(Color.parseColor("#ffffff"))
                .setContentTextSize(14)
                .setSubCalSize(14)
                .setContentTextSize(18)
                .setCancelText("全部")
                .setOnCancelClickListener {
                    tvState!!.text = "全部"
                    strState = ""
                    page = 0
                    mPresenter.getData(strState, strType, page.toString() + "", Config.PAGESIZE)
                }
                .build<Any>()
        pvNoLinkOptions.setNPicker(stateList, null, null)
        pvNoLinkOptions.show()
    }

    private fun chooseType() {
        typeList.clear()
        typeList.add("按日")
        typeList.add("按月")
        typeList.add("微策略")
        val pvNoLinkOptions: OptionsPickerView<*> = OptionsPickerBuilder(this, OnOptionsSelectListener { options1, options2, options3, v ->
            tvAll!!.text = typeList[options1]
            strType = (options1 + 1).toString() + ""
            page = 0
            mPresenter.getData(strState, strType, page.toString() + "", Config.PAGESIZE)
        }).setCancelColor(Color.parseColor("#999999"))
                .setSubmitColor(Color.parseColor("#ff3025"))
                .setTitleBgColor(Color.parseColor("#ffffff"))
                .setContentTextSize(14)
                .setSubCalSize(14)
                .setContentTextSize(18)
                .setCancelText("全部")
                .setOnCancelClickListener {
                    tvAll!!.text = "全部"
                    strType = ""
                    page = 0
                    mPresenter.getData(strState, strType, page.toString() + "", Config.PAGESIZE)
                }
                .build<Any>()
        pvNoLinkOptions.setNPicker(typeList, null, null)
        pvNoLinkOptions.show()
    }

    private fun initData(bean: AllocationManageBean) {
        list.clear()
        if (bean.respCode == Config.SUCCESS) {
            list.addAll(bean.respData!!.data!!)
            if (list.size == 0) {
                otherview!!.showEmptyView()
                refresh!!.setCanLoadMore(false)
            } else {
                refresh!!.setCanLoadMore(true)
                otherview!!.showContentView()
            }
            recycler.setLayoutManager(LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false))
            recycler.setNestedScrollingEnabled(false)
            adp = Adp(R.layout.item_allocation_manage, list)
            recycler.setAdapter(adp)
            adp.openLoadAnimation()
            adp.setOnItemClickListener(object : OnItemClickListener() {
                fun onItemClick(adapter: BaseQuickAdapter?, view: View, position: Int) {
                    val dataBean = list[position]
                    val gson = Gson()
                    strJson = gson.toJson(dataBean)
                    bundle.putString("bean", strJson)
                    toClass(view.context, DetailsAllocationActivity::class.java, bundle)
                }
            })
        } else if (bean.respCode == Config.TOKENOUT) {
            ShowDialogUtils.restLoginDialog(this)
        } else {
            ToastUtil.showToast(this, bean.respDesc)
        }
    }
}